package com.cr.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cr.api.model.AddCommunicationModel;
import com.cr.business.domain.CrCommunication;
import com.cr.business.domain.CrLawyerInfo;
import com.cr.business.domain.CrUser;
import com.cr.business.service.ICrCommunicationService;
import com.cr.common.annotation.Log;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.core.domain.Ztree;
import com.cr.common.core.domain.entity.SysDept;
import com.cr.common.core.page.TableDataInfo;
import com.cr.common.enums.BusinessType;
import com.cr.common.utils.MyStringUtils;

import io.swagger.annotations.ApiOperation;

/**
 * 留言沟通Controller
 * 
 * @author J
 * @date 2021-11-20
 */
@Controller
@RequestMapping("/business/communication")
public class CrCommunicationController extends BaseController
{
    private String prefix = "business/communication";

    @Autowired
    private ICrCommunicationService crCommunicationService;

    @RequiresPermissions("business:communication:view")
    @GetMapping()
    public String Communication(){
        return prefix + "/communication";
    }

    /**
     * 查询留言沟通列表
     */
    @RequiresPermissions("business:communication:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CrCommunication crCommunication){
        startPage();
//        Long userId = crCommunicationService.selectMinUserId();
//        crCommunication.setUserId(userId);
        List<CrCommunication> list = crCommunicationService.selectCrCommunicationList(crCommunication);
        return getDataTable(list);
    }
    
    /**
     * 查询留言的前台用户列表
     */
    @GetMapping("/userList")
    @ResponseBody
    public List<Ztree> userList(){
        List<CrCommunication> list = crCommunicationService.selectUserListList();
        return initZtreelist(list);
    }
    
    public List<Ztree> initZtreelist(List<CrCommunication> comList) {
       List<Ztree> ztrees = new ArrayList<>();
       for (CrCommunication com : comList) {
           Ztree ztree = new Ztree();
           ztree.setId(com.getUserId());
           ztree.setpId(null);
           ztree.setName(com.getUserName());
           ztree.setTitle(com.getUserName());
           ztrees.add(ztree);
       } 
       return ztrees;
     }

    

    /**
     * 新增留言
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    
    @PostMapping({ "/addCommunication" })
    @ResponseBody
    @ApiOperation("留言沟通")
    public AjaxResult addCommunication(@RequestBody AddCommunicationModel model) {
        if (model.getUserId() == null)
            return AjaxResult.error("用户id不能为空");
        if (MyStringUtils.isEmpty(model.getContent()))
            return AjaxResult.error("输入内容不能为空");
        CrCommunication communication = new CrCommunication();
        BeanUtils.copyProperties(model, communication);
        communication.setCreateBy(model.getUserId().toString());
        communication.setCreateTime(new Date());
        return toAjax(this.crCommunicationService.insertCrCommunication(communication));
    }

    /**
     * 删除留言沟通
     */
    @RequiresPermissions("business:communication:remove")
    @Log(title = "留言沟通", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(crCommunicationService.deleteCrCommunicationByIds(ids));
    }
}
